<template>
    <el-card>
        <el-table border style="margin:10px 0" :data="skuArr">
            <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
            <el-table-column label="名称" prop="skuName" show-overflow-tooltip width="150px"></el-table-column>
            <el-table-column label="描述" prop="skuDesc" show-overflow-tooltip width="150px"></el-table-column>
            <el-table-column label="图片" prop="skuDesc" width="150px">
                <template #="{ row }">
                    <img :src="row.skuDefaultImg" alt="" style="width: 100px;height: 100px;;">
                </template>
            </el-table-column>
            <el-table-column label="重量" prop="weight" width="150px"></el-table-column>
            <el-table-column label="价格" prop="price" width="150px"></el-table-column>
            <el-table-column label="操作" width="250px" fixed="right">
                <template #="{ row, $index }">
                    <el-button :type="row.isSale == 1 ? 'info' : 'primary'" size="small"
                        :icon="row.isSale == 1 ? 'Bottom' : 'Top'" title="上架SKU" @click="updateSale(row)"
                        v-has="`btn.Sku.updown`"></el-button>
                    <el-button type="success" size="small" icon="Edit" title="修改SKU" @click="updateSku(row.id)"
                        v-has="`btn.Sku.update`"></el-button>
                    <el-button type="info" size="small" icon="InfoFilled" title="查看SKU" @click="findSku(row)"
                        v-has="`btn.Sku.detail`"></el-button>
                    <el-popconfirm :title="`你确定删除这个${row.skuName}SPU吗`" width="200px">
                        <template #reference>
                            <el-button type="danger" size="small" icon="Delete" title="删除SKU"
                                @confirm="removeSku(row.id)" v-has="`btn.Sku.remove`"></el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[10, 20, 30, 40]"
            :background="true" layout="prev, pager, next, jumper,->,sizes,total" :total="total" @size-change="handler"
            @current-change="getHasSku" />
        <!-- 抽屉组件展示sku详细信息 -->
        <el-drawer v-model="drawer">
            <template #header>
                <h4>查看商品详情</h4>
            </template>
            <template #default>
                <el-row>
                    <el-col :span="6">名称</el-col>
                    <el-col :span="18">{{ skuInfo.skuName }}</el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">描述</el-col>
                    <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">价格</el-col>
                    <el-col :span="18">{{ skuInfo.price }}</el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">平台属性</el-col>
                    <el-col :span="18">
                        <el-tag v-for="item in skuInfo.skuAttrValueList" :key="item.id">{{ item.valueName }}</el-tag>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">销售属性</el-col>
                    <el-col :span="18">
                        <el-tag type="danger" v-for="item in skuInfo.skuSaleAttrValueList">{{ item.saleAttrValueName
                            }}</el-tag>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">商品图片</el-col>
                    <el-col :span="18">
                        <el-carousel :interval="4000" type="card" height="200px">
                            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                                <img :src="item.imgUrl" alt="" style="width: 100%;height: 100%;">
                            </el-carousel-item>
                        </el-carousel>
                    </el-col>
                </el-row>
            </template>
        </el-drawer>
    </el-card>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { reqSkuList, reqCancelSale, reqSaleSku, reqSkuInfo, reqRemoveSku } from '@/api/product/sku'
import type { SkuResponseData, SkuData, SkuInfoData } from '@/api/product/sku/type';
import { ElMessage } from 'element-plus';
// 分页器数据
let pageNo = ref<number>(1)
let pageSize = ref<number>(10)
let total = ref<number>(0)

// 存放已有sku列表数据
let skuArr = ref<SkuData[]>([])

// 控制抽屉显示
let drawer = ref<boolean>(false)
let skuInfo = ref<any>({})
// 组件挂在完毕获取已有sku列表
onMounted(() => {
    getHasSku()
})
const getHasSku = async (pager = 1) => {
    pageNo.value = pager
    let result: SkuResponseData = await reqSkuList(pageNo.value, pageSize.value)
    if (result.code == 200) {
        total.value = result.data.total
        skuArr.value = result.data.records
    }
}
const handler = (pageSize: number) => {
    getHasSku()
}
// 上架/下架商品
const updateSale = async (row: SkuData) => {
    //如果当前商品的isSale==1,说明当前商品是上架的额状态->更新为下架
    //否则else情况与上面情况相反
    if (row.isSale == 1) {
        //下架操作
        await reqCancelSale((row.id as number));
        //提示信息
        ElMessage({ type: 'success', message: '下架成功' });
        //发请求获取当前更新完毕的全部已有的SKU
        getHasSku(pageNo.value);

    } else {
        //上架操作
        await reqSaleSku((row.id as number));
        //提示信息
        ElMessage({ type: 'success', message: '上架成功' });
        //发请求获取当前更新完毕的全部已有的SKU
        getHasSku(pageNo.value);
    }
}
// 更新已有的sku
const updateSku = (id: number) => {
    ElMessage.success('程序员在努力的更新中。。。。')
}
// 查看商品详情的按钮事件
const findSku = async (row: SkuData) => {
    drawer.value = true
    let result: SkuInfoData = await reqSkuInfo(row.id as number)
    skuInfo.value = result.data
}
// 删除某个已有的SKU
const removeSku = async (id: number) => {
    //删除某一个已有商品的情况
    let result: any = await reqRemoveSku(id);
    if (result.code == 200) {
        //提示信息
        ElMessage({ type: 'success', message: '删除成功' });
        //获取已有全部商品
        getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1);
    } else {
        //删除失败
        ElMessage({ type: 'error', message: '系统数据不能删除' });
    }
}
</script>
<style lang="scss" scoped>
.el-row {
    margin: 12px 0;

    .el-tag {
        margin: 4px;
    }

    .el-carousel__item h3 {
        color: #475669;
        opacity: 0.75;
        line-height: 200px;
        margin: 0;
        text-align: center;
    }

    .el-carousel__item:nth-child(2n) {
        background-color: #99a9bf;
    }

    .el-carousel__item:nth-child(2n + 1) {
        background-color: #d3dce6;
    }
}
</style>